const jwt = require('jsonwebtoken')
const { updateDeptByID,getAll,verifyCode,verifyName,addDept,getdeptDetail,delDeptByid } = require('../db/dept')
const { result } =require('../config/result')
const {JWT_SECRET} = require('../config/config.dev')
class DeptConntroller{

  async updateDeptByID(ctx){
    try{

      const ret =  await updateDeptByID(ctx.request.body) 
      if(ret && ret.length>0){
        result.success = true 
        result.message = "更新部门成功"
        result.data = ret
        
      }
      else{
        result.success = false
        result.message = '更新部门不存在'
        result.data = null
      }  
      ctx.body = result
    }catch(e){
        ctx.app.emit('error',e.message,ctx)  
    } 
  }

  async getdeptDetail(ctx){
    try{
      const {id}= ctx.request.params
      const ret =  await getdeptDetail(id) 
      if(ret && ret.length>0){
        result.success = true 
        result.message = "获取部门详情成功"
        result.data = ret 
      }
      else{
        result.success = false
        result.message = '查询部门不存在'
        result.data = null
      }  
      ctx.body = result
    }catch(e){
        ctx.app.emit('error',e.message,ctx)  
    } 
  }

  async deldeptByID(ctx){
    try{
      const {id}= ctx.request.params
      const ret =  await delDeptByid(id) 
      if(ret && ret.length>0){
        result.success = true 
        result.message = "删除成功"
        result.data = ret
        
      }
      else{
        result.success = false
        result.message = '删除失败'
        result.data = null
      }  
      ctx.body = result
    }catch(e){
        ctx.app.emit('error',e.message,ctx)  
    } 
  }

  async addDept(ctx){
    try{
      const dept= ctx.request.body
      const ret =  await addDept(dept) 
      if(ret && ret.length>0){
        result.success = true 
        result.message = "添加部门成功"
        result.data = ret
        
      }
      else{
        result.success = false
        result.message = '添加部门失败'
        result.data = null
      }  
      ctx.body = result
    }catch(e){
        ctx.app.emit('error',e.message,ctx)  
    } 
  }
  async verifyName(ctx){
    try{
      const { name } = ctx.request.body
      const ret =  await verifyName(name) 
      if(ret && ret.length>0){
        result.success = false
        result.message = "部门名称已存在"
        result.data = ret
        ctx.body = result
      }
      else{
        result.message = '部门名称可以使用'
        result.success = true
        result.data = '部门名称可以使用'
        ctx.body = result
      }  
    }catch(e){
        ctx.app.emit('error',e.message,ctx)  
    } 
  }
  async verifyCode(ctx){
    try{
      const { code } = ctx.request.body
      const ret =  await verifyCode(code) 
      if(ret && ret.length>0){
        result.success = false
        result.message = "部门编码重复"
        result.data = ret
        ctx.body = result
      }
      else{
        result.data = null
        result.message = '编码可以使用'
        result.success = true
        result.data = '编码可以使用'
        ctx.body = result
        
      }  
    }catch(e){
        result.data = null
        result.message = e.message
        result.success = false
        ctx.app.emit('error',e.message,ctx)  
    } 
  }
  async getDepts(ctx){

    try{
      const ret =  await getAll() 
      if(ret && ret.length>0){
        result.success = true
        result.message = "获取部门列表成功"
        result.data = ret
        ctx.body = result
      }
      else{
        result.data = null
        result.message = '获取部门列表失败'
        result.success = true
        ctx.app.emit('error','获取部门列表失败',ctx)  
        return
      }  
    }catch(e){
        result.data = null
        result.message = e.message
        result.success = false
        ctx.app.emit('error',e.message,ctx)  
    }
      
  }
}

module.exports= new DeptConntroller()